package leetcode;

import java.util.Arrays;

/**
 * 练习题.
 * 1.两数之和
 *
 * @author :yanlixiong
 * @date :2024-02-25 11:50:00
 */
public class Exercises2 {

    public static void main(String[] args) {
        int[] nums1 = {2,7,11,15};
        int target1 = 9;
        println(nums1,target1);

        int[] nums2 = {3,2,4};
        int target2 = 6;
        println(nums2,target2);

        int[] nums3 = {3,3};
        int target3 = 6;
        println(nums3,target3);
    }

    public static void println(int[] nums,int target) {
        System.out.println("输入：nums=" + Arrays.toString(nums)+",target="+target);
        System.out.println("输出：" + Arrays.toString(twoSum(nums,target)));
        System.out.println("");
    }


    public static int[] twoSum(int[] nums, int target) {
        //先定义一个容量为2的int数组
        int[] rs = new int[2];
        //然后进行双层for循环，对数组中不同的元素进行相加，如果等于目标值，那么返回两个元素对应的下标值
        for (int i = 0; i <nums.length ; i++) {
            for (int j = 0; j <nums.length ; j++) {
                if(i!=j){
                    if(nums[i]+nums[j]==target){
                        rs[0]= i;
                        rs[1]= j;
                        return rs;
                    }
                }
            }
        }
        return rs;
    }
}
